package com.yunxin.admin.system.api.core.customer;

import com.yunxin.admin.system.infra.common.constant.SecurityConstants;
import com.yunxin.admin.system.infra.framework.security.util.SecurityUtils;
import com.yunxin.common.result.Result;
import com.yunxin.core.admin.treeUtil.AdminOptionDTO;
import com.yunxin.core.customer.domain.CustomerTag;
import com.yunxin.core.customer.domain.TagInfo;
import com.yunxin.core.customer.dto.AddTagDto;
import com.yunxin.core.customer.dto.TagTreeDto;
import com.yunxin.core.customer.dto.UpdateTagDto;
import com.yunxin.core.customer.mapper.CustomerTagMapper;
import com.yunxin.core.customer.service.TagInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "12.客户标签管理")
@Slf4j
@RestController
@RequestMapping("/core/v1/customer/tag")
@RequiredArgsConstructor
public class CustomerTagController {

    @Autowired
    private TagInfoService tagInfoService;




    @Operation(summary = "标签树", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/tree")
    public Result<List<AdminOptionDTO<Long>>> listTagOptions(@RequestParam(value = "customerId",required = false) Long customerId,
                                                             @RequestParam(value = "teamId") Integer teamId)  {
        List<AdminOptionDTO<Long>> list = tagInfoService.listDeptOptions(customerId,teamId);
        return Result.success(list);
    }

    @Operation(summary = "新增标签", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPerm('boke:tagInfo:add')")
    public Result add(@Validated @RequestBody AddTagDto addTagDto)  {
        Long userId = SecurityUtils.X.getUser().getUserId();
        addTagDto.setCreator(userId);
        tagInfoService.addTag(addTagDto);
        return Result.success();
    }

    @Operation(summary = "修改标签", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPerm('boke:tagInfo:update')")
    public Result update(@Validated @RequestBody UpdateTagDto updateTagDto)  {
        Long userId = SecurityUtils.X.getUser().getUserId();
        updateTagDto.setEditor(userId);
        tagInfoService.updateTag(updateTagDto);
        return Result.success();
    }

    @Operation(summary = "删除标签", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @DeleteMapping("/delete")
    @PreAuthorize("@ss.hasPerm('boke:tagInfo:delete')")
    public Result delete(@RequestParam(value = "tagId") Long tagId)  {
        tagInfoService.deleteTag(tagId);
        return Result.success();
    }


    @Operation(summary = "系统客户标签", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/tree/customer")
    public Result<List<Long>> listCustomerTagOptions(@RequestParam(value = "customerId",required = false) Long customerId)  {
        return Result.success(tagInfoService.listCustomerTagOptions(customerId));
    }

    @Operation(summary = "企微客户标签", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/tree/wechat")
    public Result<List<AdminOptionDTO<String>>> listQwCustomerTagOptions(@RequestParam(value = "wechatCustomerId",required = false) Long wechatCustomerId)  {
        List<AdminOptionDTO<String>> list = tagInfoService.listQwCustomerTagOptions(wechatCustomerId);
        return Result.success(list);
    }



}
